package com.zy.explore.license.domain.jpa.entity;

import com.zy.explore.license.infrastructure.base.jpa.BaseEntity;
import com.zy.explore.license.interfaces.frm.SortFieldFrm;
import com.zy.explore.license.domain.jpa.repository.JpaExampleRepository;
import com.zy.explore.license.interfaces.frm.ExampleQueryFrm;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.criteria.Predicate;
import org.hibernate.Hibernate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Jpa示例Entity(表example)ORM实体类
 *
 * @author 🇨🇳中国印🇨🇳
 * @date 2022年04月17日 01:33:12
 * @since 1.0.0
 */
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@DynamicUpdate
@DynamicInsert
@Table(name = "example")
public class JpaExampleEntity extends BaseEntity<JpaExampleRepository, JpaExampleEntity, String> {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "JDBC")
    @Column(name = "id", insertable = false, updatable = false, nullable = false, columnDefinition = "int(11) COMMENT '表主键'")
    private String id;

    /**
     * 姓名
     */
    @Column(name = "name")
    private String name;

    /**
     * 地址
     */
    @Column(name = "address")
    private String address;

    private static JpaExampleRepository repository() {
        return Holder.REPO;
    }

    private static final class Holder {
        private static final JpaExampleRepository REPO = getRepository(JpaExampleRepository.class, JpaExampleEntity.class);
    }
	
	/**
     * Frm转Specification查询条件
     *
     * @param frm 查询参数对象
     * @return 返回JPA所需的查询条件对象
     */
    public static Specification<JpaExampleEntity> getSpecification(ExampleQueryFrm frm) {
        if (frm.getPageNum() == null || frm.getPageNum() <= 0) {
            frm.setPageNum(1);
        }
        if (frm.getPageSize() == null || frm.getPageSize() <= 0) {
            frm.setPageSize(10);
        }
        return (root, query, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();
            if (!StringUtils.isEmpty(frm.getId())) {
                predicateList.add(criteriaBuilder.equal(root.get("id"), frm.getId()));
            }
            if (!StringUtils.isEmpty(frm.getName())) {
                predicateList.add(criteriaBuilder.like(root.get("name"), "%" + frm.getName() + "%"));
            }
            if (!StringUtils.isEmpty(frm.getAddress())) {
                predicateList.add(criteriaBuilder.like(root.get("address"), "%" + frm.getAddress() + "%"));
            }
            if (!StringUtils.isEmpty(frm.getCreateTimeBegin())) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime").as(String.class), frm.getCreateTimeBegin()));
            }
            if (!StringUtils.isEmpty(frm.getCreateTimeEnd())) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("createTime").as(String.class), frm.getCreateTimeEnd()));
            }
            if (!StringUtils.isEmpty(frm.getUpdateTimeBegin())) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("updateTime").as(String.class), frm.getUpdateTimeBegin()));
            }
            if (!StringUtils.isEmpty(frm.getUpdateTimeEnd())) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("updateTime").as(String.class), frm.getUpdateTimeEnd()));
            }
            return criteriaBuilder.and(predicateList.toArray(new Predicate[0]));
        };
    }
	
	@Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) {
            return false;
        }
        JpaExampleEntity entity = (JpaExampleEntity) o;
        return id != null && Objects.equals(id, entity.id);
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}